﻿namespace Autodesk.Revit.DB;

/// <summary>
/// Autodesk.Revit.DB.Curve的扩展方法
/// </summary>
public static class CurveExtensions
{
    /// <summary>
    /// 转换成模型线
    /// </summary>
    /// <param name="curve"></param>
    /// <param name="doc"></param>
    /// <param name="isFamilyCreate">是否族内创建</param>
    /// <param name="plane"></param>
    /// <returns></returns>
    public static ModelCurve ConvertToModelCurve(this Curve curve, Document doc, bool isFamilyCreate = false, Plane? plane = null)
    {
        if (plane is null)
        {
            if (curve is Line line)
            {
#if rvt16
                plane = new Plane(line.Direction.GetVerticalDirection(), line.Origin);
#else
                plane = Plane.CreateByNormalAndOrigin(line.Direction.GetVerticalDirection(), line.Origin);
#endif
            }
            else if (curve is Arc arc)
            {
#if rvt16
                plane = new Plane(arc.Normal, arc.Tessellate().First());
#else
                plane = Plane.CreateByNormalAndOrigin(arc.Normal, arc.Tessellate().First());
#endif
            }
            else
            {
                throw new NotImplementedException("'plane'参数为null，函数在自行创建时需要'curve'参数必须是 Line或Arc类型");
            }
        }

        return isFamilyCreate ? doc.FamilyCreate.NewModelCurve(curve, SketchPlane.Create(doc, plane)) : doc.Create.NewModelCurve(curve, SketchPlane.Create(doc, plane));
    }

    /// <summary>
    /// 转换成详图线
    /// </summary>
    /// <param name="curve"></param>
    /// <param name="doc"></param>
    /// <param name="view"></param>
    /// <param name="isFamilyCreate"></param>
    /// <returns></returns>
    public static DetailCurve ConvertToDetailCurve(this Curve curve, Document doc, View? view = null, bool isFamilyCreate = false)
    {
        view = view ?? doc.ActiveView;

        if (isFamilyCreate)
        {
            return doc.FamilyCreate.NewDetailCurve(view, curve);
        }
        else
        {
            return doc.Create.NewDetailCurve(view, curve);
        }
    }
}
